package com.hunmengwl.oauth.controller;

import com.hunmengwl.oauth.common.enums.StatusEnum;
import com.hunmengwl.oauth.common.util.Result;
import com.hunmengwl.oauth.dto.LockDTO;
import com.hunmengwl.oauth.entity.LockEntity;
import com.hunmengwl.oauth.service.ILockService;
import lombok.extern.log4j.Log4j2;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
import org.springframework.web.bind.annotation.*;

/**
 * @author hunmeng
 */
@RestController
@Log4j2
public class LockController {

    @Autowired
    private ILockService lockService;

    @RequestMapping("/lock")
    public String lock(String user) {
        String result = "";
        try {
            result = lockService.oauthLock(user);
            return result;
        } catch (Exception e) {
            log.error(e.getMessage(), e);
        }
        return result;
    }

    @PostMapping("/adds")
    public Result adds(@RequestBody LockDTO lockDTO){
        try {
            return Result.success(lockService.adds(lockDTO));
        }catch (Exception e){
            log.error(e.getMessage(),e);
            return Result.error(e.getMessage());
        }
    }

    @PostMapping("/import")
    public Result importLock(@RequestBody LockDTO lockDTO){
        try {
            lockService.importLock(lockDTO);
            return Result.success();
        }catch (Exception e){
            log.error(e.getMessage(),e);
            return Result.error(e.getMessage());
        }
    }

    @GetMapping("/camilleList")
    public Result list(LockEntity lockEntity, int page, int size){
        try {
            Page<LockEntity> byPage = lockService.findByPage(lockEntity, PageRequest.of(page - 1, size));

            return Result.success(StatusEnum.SUCCESS, byPage.getContent(), (int) byPage.getTotalElements());
        }catch (Exception e){
            log.error(e.getMessage(),e);
            return Result.error(e.getMessage());
        }
    }

    @GetMapping("/delete")
    public Result delete(String value){
        try {
            lockService.delete(value);
            return Result.success();
        }catch (Exception e){
            log.error(e.getMessage(),e);
            return Result.error(e.getMessage());
        }
    }

}
